<?php
/**
 * @desc 文件上传管理
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class UploadFileController extends Controller{
	//图片上传
	public function actionImageUpload(){
		$uploadedimage = CUploadedFile::getInstanceByName('Filedata');
		//检查是否传输错误
		if($uploadedimage->hasError){
			$this->output['status'] = 10006;
			$this->renderJson($this->output);
		}
		//检查大小超限制
		if($uploadedimage->getSize() > 1024*1024*1){
			$this->output['status'] = 10004;
			$this->renderJson($this->output);
		}
		//检查文件类型
		if(!in_array($uploadedimage->extensionName, array('png','jpg','gif', 'jpeg'))){
			$this->output['status'] = 10005;
			$this->renderJson($this->output);
		}
		$file_dir = 'images/upload/image/'.date("Y").'/'.date("m").'/'.date("d");
		if(!is_dir($file_dir)){
			mkdir($file_dir, 0777, true);
		}
		$file_name = $file_dir.'/'.(microtime(true)*10000).'.png';
		//文件移动
		$info = $uploadedimage->saveAs($file_name);
		if(!$info){
			$this->output['status'] = 10007;
			$this->renderJson($this->output);
		}
		$this->output['code'] = 200;
		$this->output['forwardUrl'] = Yii::app()->request->hostInfo.'/'.$file_name;
		$this->renderJson($this->output);
	}
	//editor文件上传
	public function actionEditorUpload(){
		$uploadedimage = CUploadedFile::getInstanceByName('filedata');
		header('Content-type: text/html; charset=UTF-8');
		//检查是否传输错误
		if($uploadedimage->hasError){
			exit(json_encode(array('err'=>10006, 'msg' =>"文件传输错误")));
		}
		//检查大小超限制
		if($uploadedimage->getSize() > 1024*1024*10){
			exit(json_encode(array('err'=>10004, 'msg' =>"文件大小超过限制")));
		}
		//检查文件类型
		if(!in_array($uploadedimage->extensionName, array('zip','rar','txt','jpg','jpeg','gif','png','swf'))){
			exit(json_encode(array('err'=>10005, 'msg' =>"文件类型错误")));
		}
		$file_dir = 'images/upload/image/'.date("Y").'/'.date("m").'/'.date("d");
		if(!is_dir($file_dir)){
			mkdir($file_dir, 0777, true);
		}
		$file_name = $file_dir.'/'.(microtime(true)*10000).'.png';
		//文件移动
		$info = $uploadedimage->saveAs($file_name);
		if(!$info){
			exit(json_encode(array('err'=>10007, 'msg' =>"文件移动失败")));
		}
        exit(json_encode(array('err'=>0, 'msg'=>Yii::app()->request->hostInfo.'/'.$file_name)));
	}
	//图片上传插件
	public function actionPlugin(){
		$this->render('plugin');
	}
	public function actionPluginUpload(){
		$uploadedimage = CUploadedFile::getInstanceByName('Filedata');
		//检查是否传输错误
		if($uploadedimage->hasError){
			exit(json_encode(array('err'=>10006, 'msg' =>"文件传输错误")));
		}
		//检查大小超限制
		if($uploadedimage->getSize() > 1024*1024*1){
			exit(json_encode(array('err'=>10004, 'msg' =>"文件大小超过限制")));
		}
		//检查文件类型
		if(!in_array($uploadedimage->extensionName, array('png','jpg','gif', 'jpeg'))){
			exit(json_encode(array('err'=>10005, 'msg' =>"文件类型错误")));
		}
		$file_dir = 'images/upload/image/'.date("Y").'/'.date("m").'/'.date("d");
		if(!is_dir($file_dir)){
			mkdir($file_dir, 0777, true);
		}
		$file_name = $file_dir.'/'.(microtime(true)*10000).'.png';
		//文件移动
		$info = $uploadedimage->saveAs($file_name);
		if(!$info){
			exit(json_encode(array('err'=>10007, 'msg' =>"文件移动失败")));
		}
		exit(json_encode(array('id'=>1001, 'fileName'=>Yii::app()->request->hostInfo.'/'.$file_name,'attachmentPath'=>Yii::app()->request->hostInfo.'/'.$file_name,'attachmentSize'=>1024)));
	}
}